﻿namespace Sidvall.Data.Entity
{
	public static class SpatialManager
	{
		#region Public Members

		#region GetEntityGeography

		public static System.Data.Entity.Spatial.DbGeography GetEntityGeography(Sidvall.Data.Geography item)
		{
			if (item != null)
			{
				if (item.BinaryValue != null)
					return System.Data.Entity.Spatial.DbGeography.FromBinary(item.BinaryValue, item.CoordinateSystemId);
				else if (!string.IsNullOrWhiteSpace(item.GmlValue))
					return System.Data.Entity.Spatial.DbGeography.FromGml(item.GmlValue, item.CoordinateSystemId);
				else if (!string.IsNullOrWhiteSpace(item.TextValue))
					return System.Data.Entity.Spatial.DbGeography.FromText(item.TextValue, item.CoordinateSystemId);
			}
			return null;
		}

		#endregion
		#region GetEntityGeometry

		public static System.Data.Entity.Spatial.DbGeometry GetEntityGeometry(Sidvall.Data.Geometry item)
		{
			if (item != null)
			{
				if (item.BinaryValue != null)
					return System.Data.Entity.Spatial.DbGeometry.FromBinary(item.BinaryValue, item.CoordinateSystemId);
				else if (!string.IsNullOrWhiteSpace(item.GmlValue))
					return System.Data.Entity.Spatial.DbGeometry.FromGml(item.GmlValue, item.CoordinateSystemId);
				else if (!string.IsNullOrWhiteSpace(item.TextValue))
					return System.Data.Entity.Spatial.DbGeometry.FromText(item.TextValue, item.CoordinateSystemId);
			}
			return null;
		}

		#endregion

		#region GetGeography

		public static Sidvall.Data.Geography GetGeography(System.Data.Entity.Spatial.DbGeography item, SpatialMode mode)
		{
			if (item == null)
				return null;
			switch (mode)
			{
				case SpatialMode.Gml:
					return Sidvall.Data.Geography.FromGml(item.CoordinateSystemId, item.AsGml());
				case SpatialMode.Text:
					return Sidvall.Data.Geography.FromText(item.CoordinateSystemId, item.AsText());
				default:
					return Sidvall.Data.Geography.FromBinary(item.CoordinateSystemId, item.AsBinary());
			}
		}

		#endregion
		#region GetGeometry

		public static Sidvall.Data.Geometry GetGeometry(System.Data.Entity.Spatial.DbGeometry item, SpatialMode mode)
		{
			if (item == null)
				return null;
			switch (mode)
			{
				case SpatialMode.Gml:
					return Sidvall.Data.Geometry.FromGml(item.CoordinateSystemId, item.AsGml());
				case SpatialMode.Text:
					return Sidvall.Data.Geometry.FromText(item.CoordinateSystemId, item.AsText());
				default:
					return Sidvall.Data.Geometry.FromBinary(item.CoordinateSystemId, item.AsBinary());
			}
		}

		#endregion

		#endregion
	}
}
